/**
 * 地图上的搜索栏
 * Created by LX on 2015/9/5.
 */

var searchResult=null;

//获取JSON数据并展示
function SearchHeatJsonDataAndDraw(keyword){
    //清理
    ClearSearchResult();

    //var jsonData=
    var requestData={};
    $.ajax({
        url: "static/appdata/ListHeatingData.json",
        data:requestData,
        cache: false,
        type:"GET",
        success: function(data){
            searchResult=dojo.clone(searchResult);
            //alert(JSON.stringify(data));
            if(data.success==true){
                //弹出列表框
                ShowSearchResultInfoGrid(data);

            }
        }
    });


}
//信息列表框
function ShowSearchResultInfoGrid(data){
    var jqdivInfoPanel=$("#divInfoPanel");
    var jqDivGridHeatSource=$("#divGridHeatSource");
    var jqDivGridHeatStation=$("#divGridHeatStation");

    if(!data || !data.data){
        return;
    }
    //整理数据
    var dataSources=null;
    var dataStations=null;
    $.each(data.data, function (index,item) {
        if(item.heat_type==1){
            dataSources=item;
        }
        else if(item.heat_type==2){
            dataStations=item;
        }
    });

    //开始组装
    //热源
    var htmlContent="";
//    htmlContent+='<ul class="list-group">';
//    $.each(data.data,function(index,item){
//        var aLink='<a href="javascript:LocateAndShowMarker('+item.id+')">'+item.name+'</a>';
//        htmlContent+='<li class="list-group-item">'+aLink+'</li>';
//    });
//    htmlContent+='</ul>';
//    jqDivGrid.append(htmlContent);

    if(dataSources && dataSources.children && dataSources.children.length>0){
        //绘制图元
        DrawSearchResultMarkers(dataSources);

        $.each(dataSources.children,function(index,item){
            htmlContent+=GetHeatUnitCollapseHtml(item,index);
        });
        jqDivGridHeatSource.append(htmlContent);
    }

    //换热站
    //divGridHeatSource
    htmlContent="";
    if(dataStations && dataStations.children && dataStations.children.length>0){
        //绘制图元
        DrawSearchResultMarkers(dataStations);

        $.each(dataStations.children,function(index,item){
            htmlContent+=GetHeatUnitCollapseHtml(item,index);
        });
        jqDivGridHeatStation.append(htmlContent);
    }

    //jqDivGrid.append("呵呵");
    jqdivInfoPanel.show();
}

//清理图元
function ClearSearchResult(){
    if(layerForSearchMarkers){
        layerForSearchMarkers.clear();
    }

    var jqDivGridHeatSource=$("#divGridHeatSource");
    var jqDivGridHeatStation=$("#divGridHeatStation");
    //清理数据
    jqDivGridHeatSource.children().remove();
    jqDivGridHeatStation.children().remove();
}

//绘制图元
function DrawSearchResultMarkers(data){
    if(data && data.children){
        $.each(data.children,function(index,item){
            layerForSearchMarkers.add(GetInfoMarker(item));
        });
    }
}
//信息图元
function GetInfoMarker(data){
    var point=new esri.geometry.Point(data.lat,data.lng);
    var symbol=markerSymbols.markerSymbolBlue;
    if(data.heat_type==2){
        symbol=markerSymbols.markerSymbolGreen;
    }
    else if(data.heat_type==3){
        symbol=markerSymbols.markerSymbolRed;
    }
    var g=new esri.Graphic(point, symbol);
    //g.optionData= dojo.clone(data);
//        g.setAttributes({"title":data.title});
    g.setAttributes(dojo.clone(data));

    //point.setInfoTemplate(new esri.InfoTemplate("Census Block: ${TRACT}/${BLOCK}", "Pop. in 2000: ${POP2000}"));
    //g.setInfoTemplate(new esri.InfoTemplate(data.title,data.title));
    var infoTemplate = new esri.InfoTemplate("${name}", data.name);
    g.setInfoTemplate(infoTemplate);

    return g;
}

//定位图元
function LocateAndShowMarker(id){
    //在地图上查找
    $.each(layerForSearchMarkers.graphics,function(index,item){
        if(item.attributes.id==id){
            //地图居中，弹出info
            map.centerAndZoom(item.geometry);
            item.show();
            //item.getNode().click();
            //item.showInfoWindowOnClick();
            //弹出图元信息框,（没有找到好的解决办法）
            //map.setInfoWindow(item.getInfoTemplate());
            //DrawInfoWindowForMarker(item.attributes);

            //弹出模态窗口
            //ShowWindowModel(item);

            return true;
        }
    });
}


//获得供热单元的折叠的HTML，例如某个换热站的详细信息
function GetHeatUnitCollapseHtml(data,index){
    var result="";
    var unitRowNum=data.row_no||1;
    var unitId=data.id + "";
    var unitName=data.name || "";
    var unitLinkUrl="";

    //第一个展开
    var isShowCollapseItem=false;
    if(index==0){
        isShowCollapseItem=true;
    }
    var htmlIsShowCollapseItem="";
    if(isShowCollapseItem==true){
        htmlIsShowCollapseItem="in";
    }

    var unitRunStatusHtml=GetUnitRunStatusHtml(data);

    var htmlTemp ='<div class="panel panel-default">\
        <div class="panel-heading my-collapse-panel-heading" role="tab" id="heading-heat-'+unitId+'">\
                <span>'+unitRowNum+'</span>\
        <a role="button" data-toggle="collapse"  href="#collapse-heat-'+unitId+'" aria-expanded="true" aria-controls="collapse-heat-'+unitId+'"\
        title="查看详细信息">\
                '+unitName+'\
                </a>\
                <button type="button" class="btn btn-info btn-xs"  style="float: right;" onclick="ShowWindowModelForHeatDetail(\''+data.id+'\');">详细信息</button>\
        </div>\
        <div id="collapse-heat-'+unitId+'" class="panel-collapse collapse '+htmlIsShowCollapseItem+' " role="tabpanel" aria-labelledby="heading-heat-'+unitId+'">\
                <div class="panel-body text-left">\
                <ul class="list-group my-list-group-simple">\
                <li class="list-group-item">\
                <img src="static/images/heating/u43.png" alt="" class="img-rounded my-icon-small">\
                <a role="button"  href="javascript:LocateAndShowMarker(\''+data.id+'\');">\
                '+data.address+'\
                </a>\
        </li>\
        <li class="list-group-item">\
                <img src="static/images/heating/u45.png" alt="" class="img-rounded my-icon-small">\
                运行状态\
                '+unitRunStatusHtml+'\
        </li>\
        <li class="list-group-item">\
                <img src="static/images/heating/u47.png" alt="" class="img-rounded my-icon-small">\
                '+data.manage_contact_name+'  '+data.manage_contact_tel+'\
        </li>\
        </ul>\
        </div>\
        </div>\
        </div> ';

    result=htmlTemp;

    return result;

}

//运行状态标识Html
function GetUnitRunStatusHtml(data){
    var result="";
//    <!-- Standard button -->
//    <button type="button" class="btn btn-default">（默认样式）Default</button>
//
//    <!-- Provides extra visual weight and identifies the primary action in a set of buttons -->
//    <button type="button" class="btn btn-primary">（首选项）Primary</button>
//
//    <!-- Indicates a successful or positive action -->
//    <button type="button" class="btn btn-success">（成功）Success</button>
//
//    <!-- Contextual button for informational alert messages -->
//    <button type="button" class="btn btn-info">（一般信息）Info</button>
//
//    <!-- Indicates caution should be taken with this action -->
//    <button type="button" class="btn btn-warning">（警告）Warning</button>
//
//    <!-- Indicates a dangerous or potentially negative action -->
//    <button type="button" class="btn btn-danger">（危险）Danger</button>
//
//    <!-- Deemphasize a button by making it look like a link while maintaining button behavior -->
//    <button type="button" class="btn btn-link">（链接）Link</button>
    var classStatus="btn-default";
    if(data && data.operating_status){
        if(data.operating_status=="正常"){
            classStatus="btn-success";
        }
        else if(data.operating_status=="故障"){
            classStatus="btn-warning";
        }


        result='<button type="button" class="btn '+classStatus+' btn-xs" disabled="disabled" style="float: right;">'
            +data.operating_status+'</button>';
    }
    return result;
}

//弹出框
function ShowWindowModel(data){
    //弹出框模态框
//        $('#divInfoWindow').on('shown.bs.modal', function () {
//            //$('#myInput').focus()
//        });

    $('#divInfoWindow').modal('show');

}
//供热单元详细信息框
function ShowWindowModelForHeatDetail(id){
    //弹出框模态框
//        $('#divInfoWindow').on('shown.bs.modal', function () {
//            //$('#myInput').focus()
//        });

    var jqDialogBody=$("#dialogBody");
    jqDialogBody.children().remove();

    var htmlContent='<!-- Nav tabs -->\
        <ul class="nav nav-tabs myTabs" role="tablist">\
        <li role="presentation" class="active">\
            <a href="#window_tab_content_1" aria-controls="window_tab_content_1" role="tab" data-toggle="tab">基本信息</a>\
        </li>\
        <li role="presentation">\
        <a href="#window_tab_content_2" aria-controls="window_tab_content_2" role="tab" data-toggle="tab">监测信息</a>\
        </li>\
    <li role="presentation">\
        <a href="#window_tab_content_3" aria-controls="window_tab_content_3" role="tab" data-toggle="tab">监测图</a>\
    </li>\
    </ul>\
        <!-- Tab panes -->\
    <div class="tab-content">\
        <div role="tabpanel" class="tab-pane active" id="window_tab_content_1">\
        基本信息...</div>\
        <div role="tabpanel" class="tab-pane" id="window_tab_content_2">监测信息...</div>\
        <div role="tabpanel" class="tab-pane" id="window_tab_content_3">监测图...</div>\
    </div>';
    jqDialogBody.append(htmlContent);

    $('#divInfoWindow').modal('show');

}




